package tvv.levelup.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import tvv.levelup.controller.PetFactory;
import tvv.levelup.controller.PetType;
import tvv.levelup.models.Equipment;
import tvv.levelup.models.Pet;
import tvv.levelup.service.IEquipmentService;
import tvv.levelup.service.IPetEquipmentService;
import tvv.levelup.service.IPetService;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by VLAD on 02.09.2014.
 */
@Controller
public class PetManager {
    @Autowired
    private IPetService petService;
    @Autowired
    private IEquipmentService equipmentService;
    @Autowired
    private IPetEquipmentService petEquipmentService;
    @Autowired
    private PetFactory petFactory = new PetFactory();


    public Pet getPetById(int id) {
        Pet pet = petService.getPetById(id);
        List<Integer> equipments = petEquipmentService.getPetEquipments(pet);
        pet.setEquipment(equipments);
        return pet;

    }

    public Equipment saveEquipment(String name) {
        return equipmentService.saveEquipment(name);
    }

    public Pet createPet(PetType type) {
        Pet p = petFactory.createPet(type);
        petService.savePet(p);
        List<Integer> equipments = new LinkedList();
        equipments.add(1);
        //equipments.add(1);
        p.setEquipment(equipments);
        petEquipmentService.savePetEquipment(p);
        return p;
    }

    public void deletePet(Pet pet) {
        petService.deletePet(pet);
        petEquipmentService.deletePetEquipment(pet);
    }

    public void changePetEquipment(Pet pet, List<Integer> newEquipment) {
        pet.setEquipment(newEquipment);
        petEquipmentService.deletePetEquipment(pet);
        petEquipmentService.savePetEquipment(pet);
    }
}
